#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/22 0022 21:05
# @Author  : GXl
# @File    : views.py
# @Software: win10 Tensorflow1.13.1 python3.7


from flask import g
from . import user
from utils.apiutils import *
from app import auth

logger = logutils.getlogger(__file__)


"""
健康检查
"""
@user.route('/health', methods=['GET', 'POST'])
def health():
    logger.debug("****** 进入主目录 *****")
    return "OK"
"""
测试相关
"""
@user.route("/test", methods=['GET', 'POST','DELETE','PUT'])
def test():
    return jsonData("test ok")

roleObj = {
    'id': 'admin',
    'name': '管理员',
    'describe': '拥有所有权限',
    'status': 1,
    'creatorId': 'system',
    'createTime': 1497160610259,
    'deleted': 0,
    'permissions': [
        {
            'roleId': 'admin',
            'permissionId': 'dashboard',
            'permissionName': '仪表盘',
            'actions': '[{"action":"add","defaultCheck":"false","describe":"新增"},{"action":"query","defaultCheck":"false","describe":"查询"},{"action":"get","defaultCheck":"false","describe":"详情"},{"action":"update","defaultCheck":"false","describe":"修改"},{"action":"delete","defaultCheck":"false","describe":"删除"}]',
            'actionEntitySet': [
                {
                    'action': 'add',
                    'describe': '新增',
                    'defaultCheck': "false"
                },
                {
                    'action': 'query',
                    'describe': '查询',
                    'defaultCheck': "false"
                },
                {
                    'action': 'get',
                    'describe': '详情',
                    'defaultCheck': "false"
                },
                {
                    'action': 'update',
                    'describe': '修改',
                    'defaultCheck': "false"
                },
                {
                    'action': 'delete',
                    'describe': '删除',
                    'defaultCheck': "false"
                }],
            'actionList': 'null',
            'dataAccess': 'null'
        }
    ]
}


# 定义一个用户信息的模型类


@user.route('/info', methods=['GET', 'POST'])
@auth.login_required
def info():
    logger.info('***************************获取用户信息******************************')
    user = g.user
    if not user:
        return jsonerror('未找到用户，username={}'.format(user.username))
    logger.info('获取用户{}的权限信息'.format(user.username))
    tmpdict = user.as_dict()
    #过滤掉password信息
    tmpdict['password']=''
    tmpdict['role'] = roleObj
    return jsonData(tmpdict)


# 导航菜单
navlst = [
    ## dashboard
    {
        'name': 'dashboard',
        'parentId': 0,
        'id': 1,
        'meta': {
            'icon': 'home',
            'title': '书城首页',
            'show': True
        },
        'component': 'RouteView',
        'redirect': '/mybookstore/Welcome',
        'requiredRole':'none'
    },
    {
        'name': 'Welcome',
        'parentId': 1,
        'id': 2,
        'meta': {
            'title': '欢迎',
            'show': True
        },
        'component': 'Welcome',
        'path': '/mybookstore/Welcome',
        'requiredRole':'none'
    },
    {
        'name': 'Analysis',
        'parentId': 1,
        'id': 3,
        'meta': {
            'title': '访问情况',
            'show': True
        },
        'component': 'Analysis',
        'path': '/dashboard/analysis',
        'requiredRole':'none'
    },
    ## mybookstore
    {
        'name': 'mybookstore',
        'parentId': 0,
        'id': 10000,
        'meta': {
            'title': '书城阅览',
            'icon': 'book',
            'show': True
        },
        'redirect': '/dashboard/Welcome',
        'component': 'RouteView',
        'requiredRole':'user'
    },
    {
        'name': 'MyBookStoreSearch',
        'parentId': 10000,
        'id': 10001,
        'path': '/mybookstore/mybookstoresearch',
        'meta': {
            'title': '分类搜索',
            'icon': 'search',
            'show': True
        },
        'component': 'MyBookStoreSearch',
        'requiredRole':'user'
    },
    # 收藏管理
    {
        'name': 'collectionsmanager',
        'parentId': 0,
        'id': 10010,
        'meta': {
            'title': '收藏管理',
            'icon': 'star',
            'show': True
        },
        'redirect': '/exception/403',
        'component': 'RouteView',
        'requiredRole':'user'
    },
    {
        'name': 'MyBookStoreCollections',
        'parentId': 10010,
        'id': 10011,
        'path': '/mybookstore/mybookstorecollections',
        'meta': {
            'title': '我的收藏',
            'show': True
        },
        'component': 'MyBookStoreCollections',
        'requiredRole':'user'
    },
    # 购物车管理
    {
        'name': 'shoppingcartmanager',
        'parentId': 0,
        'id': 10020,
        'meta': {
            'title': '购物车管理',
            'icon': 'shopping-cart',
            'show': True
        },
        'redirect': '/exception/403',
        'component': 'RouteView',
        'requiredRole':'user'
    },
    {
        'name': 'MyBookStoreShoppingCart',
        'parentId': 10020,
        'id': 10021,
        'path': '/mybookstore/mybookstoreshoppingcart',
        'meta': {
            'title': '我的购物车',
            'show': True
        },
        'component': 'MyBookStoreShoppingCart',
        'requiredRole':'user'
    },
    # 支付管理
    {
        'name': 'paymentmanager',
        'parentId': 0,
        'id': 10030,
        'meta': {
            'title': '支付管理',
            'icon': 'qrcode',
            'show': True
        },
        'redirect': '/exception/403',
        'component': 'RouteView',
        'requiredRole':'user'
    },
    {
        'name': 'MyBookStorePayment',
        'parentId': 10030,
        'id': 10031,
        'path': '/mybookstore/mybookStorepayment',
        'meta': {
            'title': '我的待支付订单',
            'show': True
        },
        'component': 'MyBookStorePayment',
        'requiredRole':'user'
    },
    # 订单管理
    {
        'name': 'ordermentmanager',
        'parentId': 0,
        'id': 10040,
        'meta': {
            'title': '订单管理',
            'icon': 'ordered-list',
            'show': True
        },
        'redirect': '/exception/403',
        'component': 'RouteView',
        'requiredRole':'user'
    },
    {
        'name': 'MyBookStoreOrders',
        'parentId': 10040,
        'id': 10041,
        'path': '/mybookstore/mybookstoreorders',
        'meta': {
            'title': '我的订单',
            'show': True
        },
        'component': 'MyBookStoreOrders',
        'requiredRole':'user'
    },
    # 用户中心
    {
        'name': 'usermanager',
        'parentId': 0,
        'id': 10050,
        'meta': {
            'title': '用户中心',
            'icon': 'user',
            'show': True
        },
        'redirect': '/exception/403',
        'component': 'RouteView',
        'requiredRole':'user'
    },
    {
        'name': 'MyUserInfo',
        'parentId': 10050,
        'id': 10051,
        'path': '/mybookstore/MyUserInfo',
        'meta': {
            'title': '用户信息',
            'show': True
        },
        'component': 'MyUserInfo',
        'requiredRole':'user'
    },
    # 管理员页面
    {
        'name': 'adminmanager',
        'parentId': 0,
        'id': 20000,
        'meta': {
            'title': '后台管理中心',
            'icon': 'user',
            'show': True
        },
        'redirect': '/exception/403',
        'component': 'RouteView',
        'requiredRole':'admin'
    },
    {
        'name': 'Admin',
        'parentId': 20000,
        'id': 20010,
        'path': '/mybookstore/Admin',
        'meta': {
            'title': '后台管理',
            'show': True
        },
        'component': 'Admin',
        'requiredRole':'admin'
    },
    # 测试页
    {
        'name': 'Test',
        'parentId': 0,
        'id': 50000,
        'path': '/mybookstore/test',
        'meta': {
            'title': '测试',
            'icon': 'smile',
            'show': True
        },
        'component': 'Test',
        'requiredRole':'test'
    },
]


@user.route('/nav', methods=['GET', 'POST'])
@auth.login_required
def nav():
    logger.info('***************************获取菜单列表******************************')
    user=g.user
    roleid=user.roleId
    reslst=[]
    for item in navlst:
        if 'requiredRole' in item:
            if item['requiredRole']=='none' or item['requiredRole'] in roleid:
                reslst.append(item)
        else:
            reslst.append(item)
    logger.debug('reslst='.format(reslst))
    return jsonData(reslst)
